﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.party;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Command.party
{
    partial class SangoPartyHandler
    {
        public party_apply party_apply(string partycode)
        {
            string userCode = SangoManager.Authentication();

            party_apply response = ReflectionManager.CreatePojo<party_apply>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            Entities.Usr_Partymember oldmember = PartyDao.tryGetActiveMember(userCode);

            if (oldmember != null)
            {
                response.Message = "您已经加入了军团";
                return response;
            }

            Entities.Usr_Party party = PartyDao.tryGetActiveParty(partycode);

            if (party.Membercount >= party.Maxmembercount)
            {
                response.Message = "军团人数已满，无法加入";
                return response;
            }

            if (party.Nationtypecode != profile.Nationtypecode)
            {
                response.Message = "国籍不同，无法加入此军团。";
                return response;
            }

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;
            command.Begin(delegate()
            {
                Entities.Bil_Partyapply bill = PartyDao.applyParty(profile, party);

                command.Insert(bill);

                command.Commit();
            });

            response.Succeed = true;
            return response;
        }


    }
    public interface party_apply : ISangoResponse
    {
    }
}
